package com.o2o.cleaning.month.platform.ebusiness_plat.kuaishou

import com.alibaba.fastjson.{JSON, JSONObject}
import com.mongodb.spark.MongoSpark
import com.mongodb.spark.config.ReadConfig
import com.mongodb.spark.rdd.MongoRDD
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession
import org.bson.Document

object Category {

  def main(args: Array[String]): Unit = {

    val year = 2022
    val month = 8
    val database = "Kuaishou"
    val collection = "kuaishou_category"

    val source_path = s"D://test"
    //    val source_path = s"s3a://o2o-tempdata/zyf/livestreaming/kuaishou/sourceData/${year}/${month}/${collection}/"

    val spark = SparkSession.builder()
      .appName("Kuaishou")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    sc.setLogLevel("ERROR")

    val readConfig = ReadConfig(Map(
      "spark.mongodb.input.uri" -> "mongodb://root:O2Odata123!@ 192.168.6.85:27017/admin"
      , "spark.mongodb.input.database" -> s"${database}"
      , "spark.mongodb.input.collection" -> s"${collection}"))

    // 加载mongodb配置信息
    val rdd: MongoRDD[Document] = MongoSpark.load(spark.sparkContext, readConfig)

    val values: RDD[String] = rdd.map(line => {
      val nObject: JSONObject = JSON.parseObject(line.toJson())
      nObject.toString
    })
    println(s"obs路径 : ${source_path}")
    //    spark.read.json(values).show(false)
    spark.read.json(values).repartition(1).write.option("header", "true").mode("overwrite").csv("D://test")
  }

}
